package com.hjm.app.activity;

import android.util.Log;
import android.widget.Toast;

import com.hjm.app.R;
import com.hjm.java.entity.UserInfo;
import com.hjm.java.entity.UserLogin;
import com.hjm.java.entity.UserRegister;
import com.hjm.app.fragment.user.FragmentUserInfo;
import com.hjm.app.fragment.user.FragmentUserLogin;
import com.hjm.app.fragment.user.FragmentUserRegister;
import com.hjm.java.utils.HjmObjectCallBack;
import com.hjm.ui.common.TopBar;

import java.util.ArrayList;

public class UserActivity extends BaseActivity implements
        FragmentUserLogin.OnClickListener,
        FragmentUserRegister.OnClickListener,
        FragmentUserInfo.UserInfoListener {

    private static final String TAG = "UserActivity";

    private FragmentUserLogin fragmentUserLogin;
    private FragmentUserRegister fragmentUserRegister;
    private FragmentUserInfo fragmentUserInfo;

    private TopBar topBar;
    private boolean isLogin;


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    void initData() {
        mFragmentList = new ArrayList<>();

        fragmentUserLogin = new FragmentUserLogin(this);
        fragmentUserRegister = new FragmentUserRegister(this);
        fragmentUserInfo = new FragmentUserInfo(this);
        mFragmentList.add(fragmentUserLogin);
        mFragmentList.add(fragmentUserRegister);
        mFragmentList.add(fragmentUserInfo);

        if (isLogin) {
            switchFragmentPosition(2);
            topBar.setTitle("账户信息");
        } else {
            switchFragmentPosition(0);
            topBar.setTitle("登录");
        }
    }

    @Override
    int getView() {
        return R.layout.activity_user;
    }

    @Override
    void initView() {
        isLogin = getIntent().getBooleanExtra("isLogin", false);
        Log.d(TAG, "initView: isLogin = " + isLogin);
        topBar = findViewById(R.id.top_bar);
        topBar.setOnBackClickListener((v) -> {
            finish();
        });
    }

    /**
     * 点击登录监听
     * @param username
     * @param password
     */
    @Override
    public void onLoginClick(String username, String password) {
        Log.d(TAG, "onLoginClick: 点击了登录" + "\n" + username + "\n" + password);
        UserLogin userLogin = new UserLogin(username, password);
        userService.login(userLogin,
                () -> {
                    Toast.makeText(UserActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    switchFragmentPosition(2);
                    topBar.setTitle("账户信息");
                }, msg -> {
                    Toast.makeText(UserActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                });
    }

    /**
     * 登录片段点击去到注册页面按钮监听
     */
    @Override
    public void onRegisterClick() {
        switchFragmentPosition(1);
        topBar.setTitle("注册");
    }

    /**
     * 注册片段点击注册按钮监听
     * @param username
     * @param password
     */
    @Override
    public void onRegisterClicked(String username, String password) {
        Log.d(TAG, "onLoginClick: 点击了注册" + "\n" + username + "\n" + password);
        UserRegister userRegister = new UserRegister(username, password);
        userService.register(userRegister,
                () -> {
                    Toast.makeText(UserActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                    fragmentUserLogin.setUserLogin(username, password);
                    switchFragmentPosition(0);
                    topBar.setTitle("登录");
                },
                msg -> {
                    Toast.makeText(UserActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                });
    }

    /**
     * 注册界面，取消注册返回登录界面按钮监听
     */
    @Override
    public void onCancelClicked() {
        switchFragmentPosition(0);
    }

    @Override
    public void onBackClick() {
        switchFragmentPosition(0);
    }

    /**
     * 修改用户信息界面，提交用户信息按钮监听
     * @param userInfo
     */
    @Override
    public void onUserInfoCommit(UserInfo userInfo) {
        userService.updateUserInfo(userInfo,
                () -> {
                    Toast.makeText(UserActivity.this, "更新用户信息成功", Toast.LENGTH_SHORT).show();
                    finish();
                },
                msg -> {
                    Toast.makeText(UserActivity.this, "更新用户信息失败", Toast.LENGTH_SHORT).show();
                });
    }

    /**
     * 用户信息查看界面，退出登录按钮监听
     */
    @Override
    public void onUserInfoLogout() {
        Log.d(TAG, "onUserInfoLogout: 点击了退出登录");
        userService.logout(
                ()->{
                    Toast.makeText(UserActivity.this, "退出登录成功", Toast.LENGTH_SHORT).show();
                    switchFragmentPosition(0);
                    topBar.setTitle("登录");
                },
                msg -> {
                    Toast.makeText(UserActivity.this, "退出登录失败", Toast.LENGTH_SHORT).show();
                });
    }

    /**
     * 用户信息显示界面，获取用户信息监听
     * @param callBack
     */
    @Override
    public void onGetUserInfo(HjmObjectCallBack<UserInfo> callBack) {
        userService.getUserInfo(callBack);
    }
}